<template>
	<view>
		<view class="top-tabbar">
			<view class="tab-item" :class="trade_type == 0 ? 'active' : ''" @click="stateFunc(0)">
				<text :class="trade_type == 0 ? 'trade_type' : ''">全部</text>
			</view>
			<view class="tab-item" @click="stateFunc(2)" :class="trade_type == 2 ? 'active' : ''">
				<text  :class="trade_type == 2 ? 'trade_type' : ''">支出</text>
			</view>
			<view class="tab-item" @click="stateFunc(1)" :class="trade_type == 1 ? 'active' : ''">
				<text  :class="trade_type == 1 ? 'trade_type' : ''">收入</text>
			</view>
		</view>
		<view>
			<scroll-view scroll-y="true" class="scroll-Y" :style="'height:' + scrollviewHigh + 'px;'"
				lower-threshold="50" @scrolltolower="scrolltolowerFunc">
				<view v-for="(mitem,mindex) in listData" :key='mindex'>
					<view class="p-22-32">
						<view class="d-b-c mb16">
							<picker class="datapicker" fields='month' mode="date"  :end="getNowData()" @change="changeDate($event)">
								<view class="f32 fb d-s-c">
									<text class="mr16" style="margin-top:20rpx;margin-left:20rpx">{{mitem.months || '请选择'}}</text>
									<image class="jiantou-down" src="/static/icon/jiantou-down.png" mode="aspectFill">
									</image>
								</view>
							</picker>
						</view>
					</view>
					<view class="p-0-32 item-box" v-for="(item,index) in mitem.lists" :key="index">
						<view class="p-30-0 border-b-d">
							<view class="d-b-c mb12">
								<view style="color: #000000;overflow:hidden;white-space: nowrap;text-overflow: ellipsis;-o-text-overflow:ellipsis;">{{item.project_title}}<text v-if="item.trade_type.value == 1 || item.trade_type.value == 2 ">{{item.show_name}}</text></view>
								<view class="gray32 d-s-c"  :class="item.server_type == 1 ? 'dominant' : 'dominant1'"><text v-if="item.server_type == 1">+</text><text v-else>-</text>{{item.amount}}
								</view>
							</view>
							<view class="d-b-c">
								<view class="gray3 f28 fb" style="color: #C4C4C4;">{{item.time}}</view>
							</view>
						</view>
					</view>
				</view>

				<view v-if="listData.length<=0">
					<image src="/static/icon/no-reward.png" mode="aspectFit" class="no-reward"></image>
					<view class="ww100 tc f24 gray32">暂无记录</view>
				</view>
				<!-- 没有记录 -->
				<view class="d-c-c p30" v-if="listData.length == 0 && !loading">
				</view>
				<uni-load-more v-else :loadingType="loadingType"></uni-load-more>
			</scroll-view>
		</view>
	</view>

</template>

<script>
	import uniLoadMore from '@/components/uni-load-more.vue';
	export default {
		components: {
			uniLoadMore
		},
		data() {
			return {
				/*手机高度*/
				phoneHeight: 0,
				/*可滚动视图区域高度*/
				scrollviewHigh: 0,
				loading: true,
				trade_type: '0',
				monthData: '',
				/*商品列表*/
				listData: [],
				/*当前页面*/
				page: 1,
				list_rows: 15,
				detail: {
					
				}
			}
		},
		mounted() {
			this.init();
		},
		onShow() {
			this.initData();
			this.getData()
		},
		onLoad(e) {
			this.trade_type = e.trade_type;
			this.monthData = e.monthData;
		},
		computed: {
			/*加载中状态*/
			loadingType() {
				if (this.loading) {
					return 1;
				} else {
					if (this.listData.length != 0 && this.no_more) {
						return 2;
					} else {
						return 0;
					}
				}
			}
		},
		methods: {
			getNowData(){
				return new Date()
			},
			init() {
				let self = this;
				uni.getSystemInfo({
					success(res) {
						self.phoneHeight = res.windowHeight;
						// 计算组件的高度
						let view = uni.createSelectorQuery().select('.top-tabbar');
						view.boundingClientRect(data => {
							let h = self.phoneHeight - data.height;
							self.scrollviewHigh = h;
						}).exec();
					}
				});
			},
			initData() {
				this.page = 1;
				this.listData = [];
			},
			/*获取数据*/
			getData() {
				let self = this;
				let page = self.page;
				let list_rows = self.list_rows;
				self.loading = true;
				self._get('user.user/getUserTimeLog', {
					month: self.monthData,
					trade_type: self.trade_type,
					page: page || 1,
					list_rows: list_rows,
				}, function(res) {
					self.detail = res.data.detail;
					console.log(self.listData.length)
					if (self.listData.length > 0) {
						res.data.detail.monthList.forEach((item, index) => {
							let flag = false;
							self.listData.forEach((litem, lindex) => {
								if (litem.months == item.months) {
									console.log('相同月份')
									litem.lists = [...litem.lists, ...item.lists];
									flag = true;
									return
								}
							})
							if (!flag) {
								console.log('下一月')
								self.listData = self.listData.concat(item);
							}
						})
					} else {
						console.log('<=0')
						self.listData = self.listData.concat(res.data.detail.monthList);
					}
					console.log(self.listData)
					self.last_page = res.data.detail.list.last_page;
					if (res.data.detail.list.last_page <= 1) {
						self.no_more = true;
					}
					self.loading = false;
				});
			},
			/*可滚动视图区域到底触发*/
			scrolltolowerFunc() {
				let self = this;
				self.bottomRefresh = true;
				self.page++;
				self.loading = true;
				if (self.page > self.last_page) {
					self.loading = false;
					self.no_more = true;
					return;
				}
				self.getData();
			},
			stateFunc(n) {
				console.log('aaa')
				this.monthData = '';
				if (n != this.trade_type) {console.log('bbb')
					this.page = 1;
					this.listData = [];
					this.trade_type = n;
					this.getData();
				}
			},
			/* 日期选择 */
			changeDate(e) {
				this.monthData = e.detail.value;
				this.page = 1;
				this.listData = [];
				this.getData();
			},
		}
	}
</script>

<style lang="scss">
	.dominant {
		color: $dominant-color;
	}
	.dominant1 {
		color: #1B1B1B;
	}

	.bg-eb {
		background-color: #EBEBEB;
	}

	.top-tabbar {
		overflow-x: auto;
	}

	.tab-item {
		flex-shrink: 0;
		word-break: keep-all;
		padding: 0 16rpx;
		height: 56rpx;
		line-height: 56rpx;
		text-align: center;
		// background: #F6F6F6;
		border-radius: 8rpx;
		margin: 0 12rpx;
		color: rgba(0, 0, 0, 0.56);
		font-size: 24rpx;
	}

	.tab-item.active {
		background: #FFFFFF;
		color: red;
		font-weight: 600;
		font-size: 28rpx;
	}
	.trade_type {
		line-height: 93rpx;
		text-align: center;
		height: 96rpx;
		border-bottom: 3px solid #F63E36;
	}
	.tab-item.active::after {
		width: 0;
	}

	.row-line {
		height: 56rpx;
		width: 1rpx;
		background-color: #DEDEDE;
		padding: 0 1rpx;
	}

	.item-box {
		background-color: #FFFFFF;
		// border-radius: 16rpx;
		// margin: 0 32rpx;
		padding: 0 32rpx;
		margin-top: 24rpx;
	}


	.jiantou-down {
		width: 24rpx;
		height: 24rpx;
		margin-left: 18rpx;
		margin-top:20rpx;
	}

	.unset-box {
		width: 80rpx;
		height: 40rpx;
		background: rgba(0, 0, 0, 0.1);
		border: 1rpx solid rgba(0, 0, 0, 0.55);
		opacity: 1;
		border-radius: 8rpx;
		font-size: 20rpx;
		font-weight: 400;
		color: rgba(0, 0, 0, 0.56);
		line-height: 40rpx;
		text-align: center;
		margin-right: 16rpx;
	}
	.p-24-32 {
		padding: 24rpx 32rpx;
	}
	.mr16 {
		margin-right: 16rpx;
	}
	.datapicker {
		text-align:center
	}
</style>
